using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Xeneize.SolaraBL.CRM;
using Telerik.Web.UI;

namespace WIntegrator.Pages
{
    public partial class CRMMainView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (IsOutOfBand())
            //    return;
            if (!this.IsPostBack)
            {
                FillBusinessTypeCombo();
                FillGrid();
            }
        }

        #region Private Methods

        private void FillBusinessTypeCombo()
        {
           RadComboBox tmp =  (RadComboBox)this.topContent.FindControl("cmbCompanyType");
           tmp.DataSource =  CRMBusinessTypeList.GetAll();
           tmp.DataTextField = "Description";
           tmp.DataValueField = "ID";
        }
        //private bool IsOutOfBand()
        //{
        //    bool isCallback = true;
        //    bool retValue = false;
        //    isCallback = String.Equals(Page.Request.QueryString["outofband"], "true",
        //                               StringComparison.OrdinalIgnoreCase);
        //    if (isCallback)
        //    {
        //        int companyID = Convert.ToInt32(Request.QueryString["companyid"]);                
        //        CRMCompany company = new CRMCompany(companyID);                
        //        Response.Write(company.AddressBlock);
        //        Response.Flush();
        //        Response.End();
        //        retValue = true;
        //    }
        //    return retValue;
        //}

        /// <summary>
        /// Fill Grid
        /// </summary>
        private void FillGrid()
        {
            
            this.grdCustomers.DataSource = CRMCompanies.GetAll();
            this.DataBind();
        }
        #endregion 

        protected void RadMenu1_ItemClick(object sender, RadMenuEventArgs e)
        {
            switch (e.Item.Text.ToLower())
            {
                case "save":
                    {
                        SaveNewCompany();
                        break;
                    }
            }
        }

        private void SaveNewCompany()
        {
            CRMCompany tmpCompany = new CRMCompany();
            tmpCompany.CompanyName = ((RadTextBox)this.topContent.FindControl("txtCompanyName")).Text;
            tmpCompany.AddressBlock = ((RadTextBox)this.topContent.FindControl("txtCompanyAddress")).Text;
            tmpCompany.Phone = ((RadTextBox)this.topContent.FindControl("txtCompanyPhone")).Text;
            tmpCompany.Fax = ((RadTextBox)this.topContent.FindControl("txtCompanyFax")).Text;
            tmpCompany.WebSite = ((RadTextBox)this.topContent.FindControl("txtWebsite")).Text;
            tmpCompany.FtpSite = ((RadTextBox)this.topContent.FindControl("txtFtpSite")).Text;
            tmpCompany.SalesTerms = ((RadTextBox)this.topContent.FindControl("txtSalesTerms")).Text;
            tmpCompany.SalesRep = ((RadTextBox)this.topContent.FindControl("txtSalesRep")).Text;
            RadComboBox tmp = (RadComboBox)this.topContent.FindControl("cmbCompanyType");
            tmpCompany.BusinessType = new CRMBusinessType(Convert.ToInt32(tmp.SelectedValue));
            tmpCompany.Save();
            tmpCompany = null;
            //Refresh Grid
            ((RadGrid)this.leftPanel.FindControl("grdCustomers")).Rebind();
        }


    }
}
